package com.ruoyi.hbs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.hbs.domain.MafxInventoryTransferRecords;
import com.ruoyi.hbs.domain.MsfxBusinessStatistics;
import com.ruoyi.hbs.mapper.MafxInventoryTransferRecordsMapper;
import com.ruoyi.hbs.service.MafxInventoryTransferRecordsService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hbs.mapper.MsfxTerminalStatisticsMapper;
import com.ruoyi.hbs.domain.MsfxTerminalStatistics;
import com.ruoyi.hbs.service.MsfxTerminalStatisticsService;

@Service
public class MsfxTerminalStatisticsServiceImpl extends ServiceImpl<MsfxTerminalStatisticsMapper, MsfxTerminalStatistics> implements MsfxTerminalStatisticsService {

    @Autowired
    private MsfxTerminalStatisticsMapper msfxTerminalStatisticsMapper;
    @Autowired
    private MafxInventoryTransferRecordsService mafxInventoryTransferRecordsService;
    @Autowired
    private MafxInventoryTransferRecordsMapper mafxInventoryTransferRecordsMapper;

    /**
     * 按终端统计报表数据
     *
     * @return List<HbsTerminalStatistics> 统计结果列表
     */
    @Override
    public List<MsfxTerminalStatistics> syncTerminalStatisticsReport() {
        List<MsfxTerminalStatistics> terminalStatisticsReport = msfxTerminalStatisticsMapper.getTerminalStatisticsReport();
        if (CollectionUtil.isNotEmpty(terminalStatisticsReport)) {
            // 根据productId和businessId判断是更新还是新增
            for (MsfxTerminalStatistics statistics : terminalStatisticsReport) {
                LambdaQueryWrapper<MsfxTerminalStatistics> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MsfxTerminalStatistics::getProductId, statistics.getProductId())
                        .eq(MsfxTerminalStatistics::getBusinessId, statistics.getBusinessId());

                if (this.count(queryWrapper) > 0) {
                    // 存在则更新
                    this.update(statistics, queryWrapper);
                } else {
                    // 不存在则插入
                    this.save(statistics);
                }
            }
        }
        return terminalStatisticsReport;
    }


    /**
     * 终端出库补货统计
     *
     * @return
     */
    @Override
    public  List<MafxInventoryTransferRecords> syncMsfxOutboundSupplementStatistics() {
        List<MafxInventoryTransferRecords> msfxOutboundSupplementStatistics = mafxInventoryTransferRecordsMapper.getMsfxOutboundSupplementStatistics();
        if (CollectionUtil.isNotEmpty(msfxOutboundSupplementStatistics)) {
            mafxInventoryTransferRecordsMapper.truncateTable();
            mafxInventoryTransferRecordsService.saveBatch(msfxOutboundSupplementStatistics);
        }
        return msfxOutboundSupplementStatistics;
    }

    /**
     * 同步终端统计数据到伙伴云
     * @return
     */
    public List<MsfxTerminalStatistics> syncTerminalStatisticsDataList(){
        return msfxTerminalStatisticsMapper.syncTerminalStatisticsDataList();
    }
}
